package com.ccsu.joysport.service;

import com.alibaba.fastjson2.JSON;
import com.ccsu.joysport.dto.CollegeEntry;
import com.ccsu.joysport.dto.CollegeResponse;
import com.ccsu.joysport.entity.SystemConfig;
import com.ccsu.joysport.repository.SystemConfigRepository;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.json.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SystemConfigService {

    @Autowired
    private SystemConfigRepository systemConfigRepository;

    private static final String COLLEGE_NAME_KEY = "COLLEGE_NAME_KEY";

    public CollegeResponse getAllCollege() {

        SystemConfig config = systemConfigRepository.findByKey(COLLEGE_NAME_KEY);

        List<CollegeEntry> list = JSON.parseArray(config.getValue(), CollegeEntry.class);

        CollegeResponse collegeResponse = new CollegeResponse();
        collegeResponse.setList(list);

        return collegeResponse;
    }

}
